import {
  getWarehouseList,
  getCompletedWorkList,
  checkProductionReportWorkOrder,
} from "../extension/V2/OverWork";
export const myMethod = {
  // 排序
  sortChange(val) {},
  // 选中表格数据
  tableSelectionChange(val) {
    this.tableSelection = val;
    console.log(val);
  },
  // 切换条数
  handleSizeChange(val) {
    this.pageSize = val;
    this.currentPage = 1;
    this.getTableData();
  },
  // 切换页码
  handleCurrentChange(val) {
    this.currentPage = val;
    this.getTableData();
  },
  // 点击查询操作
  queryChange(val) {
    this.form = Object.assign(this.form, val);
    this.currentPage = 1;
    this.getTableData();
  },
  // 调整状态显示文案和样式
  getAuditStatus(val) {
    // 1 审核通过   0 审核不通过    null 未审核
    // 目前只有三种状态 对象最后一个元素代表null的状态，后续有新增请在对应的倒数第二个位置添加
    const typeObj = {
      0: "danger",
      1: "primary",
      2: "warning",
    };
    const infoObj = {
      0: "审核失败",
      1: "审核成功",
      2: "未审核",
    };
    return {
      type:
        val !== null
          ? typeObj[val]
          : typeObj[Object.keys(typeObj)[Object.keys(typeObj).length - 1]],
      info:
        val !== null
          ? infoObj[val]
          : infoObj[Object.keys(infoObj)[Object.keys(infoObj).length - 1]],
    };
  },
  // 重置查询数据
  resetPage() {
    this.pageSize = 15;
    this.currentPage = 1;
    this.totalPage = 0;
    this.$refs.dataTableRef.tableData = [];
    this.form = {};
  },
  // 清空弹框的数据
  closeChange() {
    this.warehouseSelection = [];
    this.warehouse = "";
    this.$refs.warehouseDialogTableRef.tableData = [];
  },
  // 获取列表数据
  async getTableData() {
    this.$refs.dataTableRef.tableLoading = true;
    try {
      const { WorkOrderCode, ProcessName } = this.form;
      const params = {
        WorkOrderCode: WorkOrderCode || "", // 工单编号
        ProcessName: ProcessName || "", // 工序名称
        rows: this.pageSize,
        page: this.currentPage,
        // sort: this.$refs.dataTableRef.sort,
        // order: this.$refs.dataTableRef.order,
      };
      const res = await getCompletedWorkList(params);
      if (res.status === 0) {
        this.$refs.dataTableRef.tableData = res.rows || [];
        this.totalPage = res.total || 0;
      } else {
        this.$refs.dataTableRef.tableData = [];
        this.totalPage = 0;
        this.$message.error(res.msg || "查询失败");
      }
      this.$refs.dataTableRef.tableLoading = false;
    } catch (err) {
      this.$message.error(err.msg || "查询失败");
      this.$refs.dataTableRef.tableLoading = false;
    }
  },
  // 点击审核操作
  async auditChange(type) {
    if (this.tableSelection.length == 0) {
      return this.$message.error("请选择要审批的行!");
    }
    if (this.tableSelection.length != 1) {
      return this.$message.error("只能选择一行数据进行审批!");
    }
    // null 未审核   0 审核失败   1 审核成功
    if (this.tableSelection[0].auditorStatus !== null) {
      const approver = `已经审批的工单${this.tableSelection[0].workOrderCode}不允许重复审批!`
      return this.$message.error(approver);
    }
    // 符合审核条件 弹出仓库选择（必选）
    this.selectAuditType = type;
    // 暂时不需要选择仓库id
    // this.$refs.dialogComRef.dialogVisible = true;
    // this.getWarehouseList({
    //   order: "desc",
    //   page: 1,
    //   rows: 10000,
    //   sort: "CreateDate",
    //   wheres: "[]",
    // });
    this.confirmAuditChange()
  },
  // 查询仓库信息
  queryWarehouseChange() {
    let wheres = [
      {
        name: "warehouseCode",
        value: this.warehouse,
        displayType: "like",
      },
    ];
    let temp = {
      order: "desc",
      page: 1,
      rows: 10000,
      sort: "CreateDate",
      wheres: this.warehouse ? JSON.stringify(wheres) : "[]",
    };
    this.getWarehouseList(temp);
  },
  // 获取仓库信息列表
  async getWarehouseList(params) {
    try {
      this.$refs.dialogComRef.loading = true;
      const result = await getWarehouseList(params);
      if (result.status === 0) {
        this.$refs.warehouseDialogTableRef.tableData = result.rows || [];
      } else {
        this.$message.error(result.msg || "获取仓库数据失败");
      }
      this.$refs.dialogComRef.loading = false;
    } catch (err) {
      this.$refs.dialogComRef.loading = false;
      console.error("获取仓库数据异常:", err);
    }
  },
  // 点击仓库多选框
  warehouseSelectionChange(val) {
    this.warehouseSelection = val;
    console.log("选中的仓库信息：", this.warehouseSelection);
  },
  // 点击确认按钮 对选中的数据进行校验
  confirmChange() {
    if (this.warehouseSelection && this.warehouseSelection.length == 0) {
      return this.$message.error("请选择要审批的行!");
    }
    if (this.warehouseSelection && this.warehouseSelection.length != 1) {
      return this.$message.error("只能选择一行数据进行审批!");
    }
    console.log("选中的仓库信息：", this.warehouseSelection);
    this.confirmAuditChange();
  },
  // 弹框确认审核操作
  async confirmAuditChange() {
    try {
      // this.$refs.dialogComRef.loading = true;
      const params = {
        reportWorkOrder_Id: this.tableSelection[0].reportWorkOrder_Id, // 报工单Id
        // myProperty: this.warehouseSelection[0].workOrder_Id, // 仓库Id 暂时不需要仓库id
        status: this.selectAuditType,
      };
      const result = await checkProductionReportWorkOrder(params);
      console.log(result);
      if (result.ok) {
        this.$message.success("审核成功");
        // this.$refs.dialogComRef.dialogVisible = false;
        // 清空查询条件
        // this.closeChange();
        this.resetPage();
        // 刷新列表数据
        this.getTableData();
      } else {
        this.$message.error((result.err&&result.err.msg) || "审核失败");
      }
      // this.$refs.dialogComRef.loading = false;
    } catch (err) {
      // this.$refs.dialogComRef.loading = false;
      console.error("数据异常", err);
    }
  },
};
